import { TrackingEvents, IBaseTrackingEvent } from '@datahub/shared/types/tracking/event-tracking';

/**
 * UI Tracking event types possible in DataHub
 */
export enum CoreUITrackingEventName {
  // PageViewEvent (PVE) is an event used to signal that a certain UI element has been shown
  PageViewEvent = 'PageViewEvent',
  // Tracks when a user interacts with a control, where a control is any physical element of the app. For instance, a control can be a button, link, or dropdown.
  ControlInteractionEvent = 'ControlInteractionEvent',
  // Tracks that the user navigated from one page/screen to another page/screen
  NavigationEvent = 'NavigationEvent'
}

/**
 * Types of page that are valid for a PageViewEvent
 * @exports
 * @enum{string}
 */
export enum PageType {
  Full = 'full',
  Ajax = 'ajax',
  Iframe = 'iframe',
  Redirect = 'redirect',
  Api = 'api',
  Form = 'form',
  Router = 'router',
  Error = 'error'
}
/**
 * String values for categories that can be tracked with the application
 * @export
 * @enum {string}
 */
export enum TrackingEventCategory {
  DatasetCompliance = 'DATASET_COMPLIANCE',
  Search = 'SEARCH',
  Entity = 'ENTITY',
  ControlInteraction = 'CONTROL_INTERACTION'
}

/**
 * List of EventNames across the app that will be tracked via one or more tracking frameworks.
 */
export enum CustomTrackingEventName {
  BrowseAction = 'DataHubBrowseActionEvent',
  SearchAction = 'DataHubSearchActionEvent',
  SearchImpression = 'DataHubSearchImpressionEvent',
  ControlInteractionEvent = 'ControlInteractionEvent'
}

/**
 * Custom identifier for the current page being viewed
 * @export
 */
export enum PageKey {
  // Identifier for the browse  category page which contains the top level browse targets i.e. data entities within  the application
  browseCategory = 'd_browse_category',
  searchCategory = 'd_search_category'
}

/**
 * List of the SearchActionEventCategories that fall under the  `DataHubSearchActionEvent` namespace.
 * The semantic type of ui event action that was performed by the user e.g. SEARCHCLICK, SEARCHSATCLICK
 *
 * @export
 * @enum {string}
 */
export enum SearchActionEventCategory {
  // Click event generated by clicking a search result
  SearchClick = 'SEARCHCLICK',
  // Satisfied Search Click event was generated i.e. search clicks excluding entity views followed by quick-backs. We infer search satisfaction based on action category and time spent on viewing the entity page (DWELL TIME). See more: http://go/datahub/unifiedmetrics
  SearchSatClick = 'SEARCHSATCLICK',
  // Search facet click event was generated by toggling a facet to active
  FacetClick = 'FACETCLICK'
}

/**
 * Enumerates the available tracking goals
 * @export
 * @enum {number}
 */
export enum TrackingGoal {
  // Satisfied Search Clicks goal tracking
  SatClick = 1
}

// Convenience alias for insertTrackingEventsCategoryFor return type
type InsertTrackingEventsCategoryForReturn = Record<string, IBaseTrackingEvent | Partial<IBaseTrackingEvent>>;

/**
 * Augments a tracking event partial with category information
 * @param {TrackingEvents} events the events mapping
 * @param {[string, Partial<IBaseTrackingEvent>]} [eventName, trackingEvent]
 */
export const insertTrackingEventsCategoryFor = (
  category: TrackingEventCategory
): ((
  events: TrackingEvents,
  eventNameAndEvent: [string, Partial<IBaseTrackingEvent>]
) => InsertTrackingEventsCategoryForReturn) => (
  events: TrackingEvents,
  [eventName, trackingEvent]: [string, Partial<IBaseTrackingEvent>]
): InsertTrackingEventsCategoryForReturn => ({
  ...events,
  [eventName]: { ...trackingEvent, category }
});
